﻿using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using HandyControl.Data;
using HandyControl.Tools;
using Koala.Pro.Helpers;
using Koala.Pro.ViewModels;
using Koala.Pro.Views.Pages;
using System.ComponentModel;
using System.Windows;
using Wpf.Ui;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;

namespace Koala.Pro.Views
{
    public partial class MainWindow : FluentWindow, INavigationWindow
    {
        private readonly INavigationService navigationService;

        private bool _isUserClosedPane;

        private bool _isPaneOpenedOrClosedFromCode;

        public MainWindowViewModel ViewModel { get; }

        public MainWindow(MainWindowViewModel viewModel, IPageService pageService, INavigationService navigationService)
        {
            ViewModel = viewModel;
            base.DataContext = this;
            SystemThemeWatcher.Watch(this);
            InitializeComponent();
            SetPageService(pageService);
            navigationService.SetNavigationControl(RootNavigation);
            this.navigationService = navigationService;
        }

        public INavigationView GetNavigation()
        {
            return RootNavigation;
        }

        public bool Navigate(Type pageType)
        {
            return RootNavigation.Navigate(pageType);
        }

        public void SetPageService(IPageService pageService)
        {
            RootNavigation.SetPageService(pageService);
        }

        public void ShowWindow()
        {
            Show();
        }

        public void CloseWindow()
        {
            Close();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            Application.Current.Shutdown();
        }

        INavigationView INavigationWindow.GetNavigation()
        {
            throw new NotImplementedException();
        }

        public void SetServiceProvider(IServiceProvider serviceProvider)
        {
            throw new NotImplementedException();
        }

        private void RootNavigation_Navigated(NavigationView sender, NavigatedEventArgs args)
        {
            //Debug.WriteLine(args.RoutedEvent.ToJson());
        }

        private void OnNavigationSelectionChanged(object sender, RoutedEventArgs e)
        {
            if (sender is NavigationView navigationView)
            {
                RootNavigation.HeaderVisibility = ((!(navigationView.SelectedItem?.TargetPageType != typeof(DarkAdmin))) ? Visibility.Collapsed : Visibility.Visible);
            }
        }

        private void MainWindow_OnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (!_isUserClosedPane)
            {
                _isPaneOpenedOrClosedFromCode = true;
                RootNavigation.IsPaneOpen = !(e.NewSize.Width <= 1200.0);
                _isPaneOpenedOrClosedFromCode = false;
            }
        }

        private void NavigationView_OnPaneOpened(NavigationView sender, RoutedEventArgs args)
        {
            if (!_isPaneOpenedOrClosedFromCode)
            {
                _isUserClosedPane = false;
            }
        }

        private void NavigationView_OnPaneClosed(NavigationView sender, RoutedEventArgs args)
        {
            if (!_isPaneOpenedOrClosedFromCode)
            {
                _isUserClosedPane = true;
            }
        }

        [RelayCommand]
        private void Back()
        {
            if (ViewModel.CanBack && (ViewModel.DisableDirtyMessage || !ViewModel.IsDirty || MessageDxUtil.ShowYesNoAndWarning("界面控件数据已编辑过，是否确认丢弃并关闭窗口") == System.Windows.MessageBoxResult.Yes))
            {
                ViewModel.IsDirty = false;
                ViewModel.CanBack = true;
                navigationService.GoBack();
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            //if (ViewModel.NotifyIconIsShow)
            //{
            //    SendNotification("托盘图标已打开，将隐藏窗口而不是关闭程序。");
            //    Hide();
            //    e.Cancel = true;
            //}
            //else
            //{
                base.OnClosing(e);
            //}
        }

        [RelayCommand]
        private void PushMainWindow2Top()
        {
            if (Application.Current.MainWindow != null)
            {
                if (Application.Current.MainWindow.Visibility != 0)
                {
                    Application.Current.MainWindow.Show();
                }
                WindowHelper.SetWindowToForeground(Application.Current.MainWindow);
                if (Application.Current.MainWindow.WindowState == WindowState.Normal)
                {
                    Application.Current.MainWindow.WindowState = WindowState.Maximized;
                }
                else
                {
                    Application.Current.MainWindow.WindowState = WindowState.Normal;
                }
            }
        }

        [RelayCommand]
        private void ShutdownApp()
        {
            Application.Current.Shutdown();
        }

        [RelayCommand]
        private void SendNotification(string content)
        {
            NotifyIcon.ShowBalloonTip("提示信息", content, NotifyIconInfoType.None, ViewModel.ContextMenuIsShow ? "NotifyIconToken" : "NotifyIconContextToken");
        }

        [RelayCommand]
        private void MouseDouble(string content)
        {
            GrowlUtil.ShowInfo(content);
        }

        private void OnNavigationSelectionChanged(NavigationView sender, RoutedEventArgs args)
        {

        }
    }
}